const state = {
  questions: [],
  questionsScore: 0,
};

const mutations = {
  /**
   *
   * @param {*} state
   * @param {*} val  数组
   * @returns
   */
  SET_QUESTIONS: (state, val) => {
    if (!Array.isArray(val)) {
      return state.questions;
    }
    state.questions.push(...val);
  },
  /**
   * 删除所有
   */
  DEL_ALL_QUESTIONS: (state) => {
    state.questions = [];
  },
  /**
   * 根据ID删除一个
   * @param {*} id
   */
  DEL_QUESTIONS_BY_ID: (state, id) => {
    state.questions = state.questions.filter((v) => v.questionId != id);
  },

  // 获取题目总分 :: TODO 在试卷详情展示时，实际state里没有题
  GET_QUESTIONS_SCORE: (state) => {
    state.questionsScore = state.questions.reduce((pre, next) => {
       
      return pre + parseInt(next.questionScore);
    }, 0);
  },
};

const actions = {
  /**
   *
   * @param {*} val 数组
   */
  setQuestions({ commit }, val) {
    commit("SET_QUESTIONS", val);
  },
  /**
   * 清理所有
   */
  delAllQuestions({ commit }) {
    commit("DEL_ALL_QUESTIONS");
  },
  /**
   * 根据questionId 删除
   * @param {*} id
   */
  delQuestionsById({ commit }, id) {
    commit("DEL_QUESTIONS_BY_ID", id);
  },

  // 获取题目总分
  getQuestionScore: ({ commit }) => {
    commit("GET_QUESTIONS_SCORE");
  },
};

export default {
  //   namespaced: true,
  state,
  mutations,
  actions,
};
